/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pp.persistance;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.pp.entites.Mobiledevice;
import com.pp.entites.MobiledeviceHasEvenement;
import com.pp.entites.MobiledeviceHasEvenementPK;
import com.pp.persistance.exceptions.NonexistentEntityException;
import com.pp.persistance.exceptions.PreexistingEntityException;
import com.pp.persistance.exceptions.RollbackFailureException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.transaction.UserTransaction;

/**
 *
 * @author arnaud
 */
public class MobiledeviceHasEvenementJpaController implements Serializable {

    public MobiledeviceHasEvenementJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(MobiledeviceHasEvenement mobiledeviceHasEvenement) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK() == null) {
            mobiledeviceHasEvenement.setMobiledeviceHasEvenementPK(new MobiledeviceHasEvenementPK());
        }
        mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK().setIdmobileDevice(mobiledeviceHasEvenement.getMobiledevice().getId());
        mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK().setIdCreateur(mobiledeviceHasEvenement.getEvenement().getEvenementPK().getIdCreateur());
        mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK().setNumEvenement(mobiledeviceHasEvenement.getEvenement().getEvenementPK().getNumEvenement());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Mobiledevice mobiledevice = mobiledeviceHasEvenement.getMobiledevice();
            if (mobiledevice != null) {
                mobiledevice = em.getReference(mobiledevice.getClass(), mobiledevice.getId());
                mobiledeviceHasEvenement.setMobiledevice(mobiledevice);
            }
            em.persist(mobiledeviceHasEvenement);
            if (mobiledevice != null) {
                mobiledevice.getMobiledeviceHasEvenementList().add(mobiledeviceHasEvenement);
                mobiledevice = em.merge(mobiledevice);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findMobiledeviceHasEvenement(mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK()) != null) {
                throw new PreexistingEntityException("MobiledeviceHasEvenement " + mobiledeviceHasEvenement + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(MobiledeviceHasEvenement mobiledeviceHasEvenement) throws NonexistentEntityException, RollbackFailureException, Exception {
        mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK().setIdmobileDevice(mobiledeviceHasEvenement.getMobiledevice().getId());
        mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK().setIdCreateur(mobiledeviceHasEvenement.getEvenement().getEvenementPK().getIdCreateur());
        mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK().setNumEvenement(mobiledeviceHasEvenement.getEvenement().getEvenementPK().getNumEvenement());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            MobiledeviceHasEvenement persistentMobiledeviceHasEvenement = em.find(MobiledeviceHasEvenement.class, mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK());
            Mobiledevice mobiledeviceOld = persistentMobiledeviceHasEvenement.getMobiledevice();
            Mobiledevice mobiledeviceNew = mobiledeviceHasEvenement.getMobiledevice();
            if (mobiledeviceNew != null) {
                mobiledeviceNew = em.getReference(mobiledeviceNew.getClass(), mobiledeviceNew.getId());
                mobiledeviceHasEvenement.setMobiledevice(mobiledeviceNew);
            }
            mobiledeviceHasEvenement = em.merge(mobiledeviceHasEvenement);
            if (mobiledeviceOld != null && !mobiledeviceOld.equals(mobiledeviceNew)) {
                mobiledeviceOld.getMobiledeviceHasEvenementList().remove(mobiledeviceHasEvenement);
                mobiledeviceOld = em.merge(mobiledeviceOld);
            }
            if (mobiledeviceNew != null && !mobiledeviceNew.equals(mobiledeviceOld)) {
                mobiledeviceNew.getMobiledeviceHasEvenementList().add(mobiledeviceHasEvenement);
                mobiledeviceNew = em.merge(mobiledeviceNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                MobiledeviceHasEvenementPK id = mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK();
                if (findMobiledeviceHasEvenement(id) == null) {
                    throw new NonexistentEntityException("The mobiledeviceHasEvenement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(MobiledeviceHasEvenementPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            MobiledeviceHasEvenement mobiledeviceHasEvenement;
            try {
                mobiledeviceHasEvenement = em.getReference(MobiledeviceHasEvenement.class, id);
                mobiledeviceHasEvenement.getMobiledeviceHasEvenementPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The mobiledeviceHasEvenement with id " + id + " no longer exists.", enfe);
            }
            Mobiledevice mobiledevice = mobiledeviceHasEvenement.getMobiledevice();
            if (mobiledevice != null) {
                mobiledevice.getMobiledeviceHasEvenementList().remove(mobiledeviceHasEvenement);
                mobiledevice = em.merge(mobiledevice);
            }
            em.remove(mobiledeviceHasEvenement);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<MobiledeviceHasEvenement> findMobiledeviceHasEvenementEntities() {
        return findMobiledeviceHasEvenementEntities(true, -1, -1);
    }

    public List<MobiledeviceHasEvenement> findMobiledeviceHasEvenementEntities(int maxResults, int firstResult) {
        return findMobiledeviceHasEvenementEntities(false, maxResults, firstResult);
    }

    private List<MobiledeviceHasEvenement> findMobiledeviceHasEvenementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(MobiledeviceHasEvenement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public MobiledeviceHasEvenement findMobiledeviceHasEvenement(MobiledeviceHasEvenementPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(MobiledeviceHasEvenement.class, id);
        } finally {
            em.close();
        }
    }

  public List<MobiledeviceHasEvenement> findByIdmobileAndADiffuser(String idmobileDevice, boolean aDiffuser) {
        EntityManager em = getEntityManager();
    try {
      return em.createNamedQuery("MobiledeviceHasEvenement.findByIdmobileAndADiffuser").setParameter("idmobileDevice",
          idmobileDevice).setParameter("aDiffuser", aDiffuser).getResultList();
    } catch (NoResultException e) {
      return null;
    }
  }
    
    
    public int getMobiledeviceHasEvenementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<MobiledeviceHasEvenement> rt = cq.from(MobiledeviceHasEvenement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
